<?php

class transport_fee extends AppModel {

    var $alias = 'transport_fee';
    var $name = 'transport_fee';
    var $useTable = 'transport_fee';
    var $belongsTo = array(
        'District' => array(
            'className' => 'District',
            'foreignKey' => 'district_id',
            'fields' => array('District.id', 'District.title')
        )
    );

    function paginateCount($conditions = null, $recursive = 0, $extra = array()) {
        $parameters = compact('conditions');
        $this->recursive = $recursive;
        $count = $this->find('count', array_merge($parameters, $extra));
        if (isset($extra['group'])) {
            $count = $this->getAffectedRows();
        }
        return $count;
    }

    function cal_fee($district_id, $total_price_order) {
        $conditions = array();
        //if we're editting
        $conditions = array(
            "{$this->alias}.district_id" => $district_id,
            "{$this->alias}.order_price <=" => $total_price_order,
        );
        $result = $this->field("{$this->alias}.fee", $conditions, "{$this->alias}.order_price DESC");
        if ($result) {
            return $result;
        } else {
            return 0;
        }
    }

}

?>
